using System.Collections.Generic;
using FineCollection.Test;
using FineCollection.Intf;
using NUnit.Framework;



namespace FineCollection.Const
{
    [TestFixture]
    public class Extensions_test
    {

        [Test]
        public void Test_InSortSet()
        {
            var set = CC1.SortSetOf(333L, 666L, 999L);

            666L.In(set) ._true_();
            111L.In(set) ._false_();
        }


        [Test]
        public void Test_InHashSet()
        {
            var set = CC1.HashSetOf(333L, 666L, 999L);

            666L.In(set) ._true_();
            111L.In(set) ._false_();
        }


        [Test]
        public void Test_InNull()
        {
            ConstHashSet<long> set = null;

            666L.In(set) ._false_();
        }


        [Test]
        public void Test_ForEachBasic()
        {
            long p = 1L;

            RList<long> numbers = CC.ListOf(1, 3L, 5L, 7L);

            numbers.ForEach(x => p *= x);

            Assert.That(p, Is.EqualTo(3 * 5 * 7));
        }


        [Test]
        public void Test_ToRLIst()
        {
            var msList = new List<long> {11111111, 22222222, 33333333, 44444444};
            IEnumerable<long> source = msList as IEnumerable<long>;

            RList<long> rlist = source.ToConstList();

            Assert.NotNull(rlist);
            Assert.AreEqual(msList.Count, rlist.Count);

            Assert.AreEqual(msList[0], rlist.First);
            Assert.AreEqual(msList[3], rlist.Last);
        }


        [Test]
        public void Test_ToRSet_0()
        {
            var msList = new List<long>(0);
            IEnumerable<long> source = msList as IEnumerable<long>;

            RSet<long> set = source.ToSortSet();

            Assert.NotNull(set);
            Assert.True(set.IsEmpty);
        }


        [Test]
        public void Test_ToRSet_1()
        {
            var msList = new List<long> { 5555 };
            IEnumerable<long> source = msList as IEnumerable<long>;

            RSet<long> set = source.ToSortSet();

            Assert.NotNull(set);
            Assert.AreEqual(1, set.Count);
            Assert.True(set.Contains(5555));
        }


        [Test]
        public void Test_ToRSet_2()
        {
            var msList = new List<long> { 3333, 7777 };
            IEnumerable<long> source = msList as IEnumerable<long>;

            RSet<long> set = source.ToSortSet();

            Assert.NotNull(set);
            Assert.AreEqual(2, set.Count);
            Assert.True(set.Contains(3333));
            Assert.True(set.Contains(7777));
        }


        [Test]
        public void Test_ToRSet_2_ordered()
        {
            var msList = new List<long> { 3333, 7777 };
            IEnumerable<long> source = msList as IEnumerable<long>;

            ROrderSet<long> set = source.ToSortSet();

            Assert.NotNull(set);
            Assert.AreEqual(2, set.Count);
            Assert.AreEqual(3333, set.First);
            Assert.AreEqual(7777, set.Last);
        }


        [Test]
        public void Test_ToRSet_3()
        {
            var msList = new List<long> { 3333, 5555, 7777 };
            IEnumerable<long> source = msList as IEnumerable<long>;

            RRandomSet<long> set = source.ToSortSet();

            Assert.NotNull(set);
            Assert.AreEqual(3, set.Count);
            Assert.AreEqual(3333, set[1]);
            Assert.AreEqual(5555, set[2]);
            Assert.AreEqual(7777, set[3]);
        }


        [Test]
        public void Test_ToRSet_5_sort()
        {
            var msList = new List<long> { 3333, 5555, 1111, 4444, 2222 };
            IEnumerable<long> source = msList as IEnumerable<long>;

            RRandomSet<long> set = source.ToSortSet();

            Assert.NotNull(set);
            Assert.AreEqual(5, set.Count);
            Assert.AreEqual(1111, set[1]);
            Assert.AreEqual(2222, set[2]);
            Assert.AreEqual(3333, set[3]);
            Assert.AreEqual(4444, set[4]);
            Assert.AreEqual(5555, set[5]);
        }


        [Test]
        public void Test_ToRSet_5_sortAndRemoveDuplicates()
        {
            var msList = new List<long> { 1111, 1111, 2222, 2222, 3333, 3333, 2222, 1111 };
            IEnumerable<long> source = msList as IEnumerable<long>;

            RRandomSet<long> set = source.ToSortSet();

            Assert.NotNull(set);
            Assert.AreEqual(3, set.Count);
            Assert.AreEqual(1111, set[1]);
            Assert.AreEqual(2222, set[2]);
            Assert.AreEqual(3333, set[3]);
        }


        [Test]
        public void Test_ToRMap_3()
        {
            long[] z = new long[] {0x000A0001, 0x000B0002, 0x000C0003};

            RMap<long, long> map = z.ToSortMap(x => x >> 16, x => x & 0xFFFF, 4);

            Assert.NotNull(map);
            Assert.AreEqual(3, map.Count);

            Assert.AreEqual(0x0001, map[0x000A]);
            Assert.AreEqual(0x0002, map[0x000B]);
            Assert.AreEqual(0x0003, map[0x000C]);
        }
            


    }
}